var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike, historyDataArr;
Page({

  data: {
    historyContent:[],
    hFlag: false,
    cFlag: true,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
  },
  onLoad:function(){},
  onShow: function (options) {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/10/search/hot ',
      data: {},
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
      },
      method: 'POSt',
      success: function (res) {
        // console.log(res.data.obj);
        var hotSearch = res.data.obj;
        that.setData({
          hotSearch: hotSearch,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    //数组去重
    // var historyDataArrs=wx.getStorage('historySearchs');   
    // console.log(historyDataArrs)
    //   var historyDataArr = [historyDataArrs[0]];
    //   for (var i = 1; i < historyDataArrs.length; i++) {
    //     if (historyDataArr.indexOf(historyDataArrs[i]) == -1) {
    //       historyDataArr.push(historyDataArrs[i]);
    //     }
    //   }
    // that.setData({
    //   historyContent: historyDataArr,
    // })  
    // console.log(wx.getStorageSync('searchData'))
    that.setData({
      historyContent: wx.getStorageSync('searchData') || [] ,
    }) 
    
  },
  inputFun: function (e) {    
    var values = e.detail.value;   
    this.searchDataFun(values);
    if (values !== '') {
      var searchData = this.data.historyContent;
      searchData.unshift({values})
      console.log(searchData);
      wx.setStorageSync('searchData', searchData);
    }
  },
  // 搜索数据
  searchDataFun: function (values){
   var that=this;  
   if (values !== '') {    
     wx.request({
       url: httpNativetalk + '/app/13/search ',
       data: {
         "currentPage": 1,
         "pageSize": 10,
         "obj": values,
       },
       header: {
         "Timestamp": timeSecond,
         "SignInfo": SignInfoData,
         "content-Type": "application/json",
         "client_type": "3",
         "version": 15,
       },
       method: 'POSt',
       success: function (res) {
        //  console.log(res);
         var teacherData = res.data.obj.teachers;
         var classData = res.data.obj.courses;
         for (var i = 0; i < teacherData.length; i++) {
           teacherData[i].tea_head_portrait = 'http://file.waijiaojun.com/' + teacherData[i].tea_head_portrait
         };
         that.setData({
           classData: classData,
           teacherData: teacherData,
           hFlag: true,
           cFlag: false,
         })
       },
       fail: function (res) { },
       complete: function (res) { },
     })
   }
  },
  //清除历史搜索记录
  clearStorage:function(){
    wx.removeStorageSync('searchData');
    this.setData({
      historyContent:[],
    })
  },
  teacherContentFun: function (e) {
    var teaIds = e.currentTarget.dataset.tea_id
    wx.navigateTo({
      url: "/pages/teacherInfor/teacherInfor?teaId=" + teaIds,
    });

  },
  classContentFun: function (e) {
    var classIds = e.currentTarget.dataset.idx;
    var titleData = e.currentTarget.dataset.titles;
    wx.navigateTo({
      url: '/pages/classDetail/classDetail?classId=' + classIds,
    });
    wx.setNavigationBarTitle({
      title: titleData,
    });
    var values = e.currentTarget.dataset.titles;
    this.searchDataFun(values);
    if (values !== '') {
      var searchData = this.data.historyContent;
      searchData.unshift({ values })
      console.log(searchData);
      wx.setStorageSync('searchData', searchData);
    }
  },
  //热门搜索
  hotSearchFun: function (e) {
    console.log(e)
    var values = e.currentTarget.dataset.text;
    this.searchDataFun(values);
    if (values !== '') {
      var searchData = this.data.historyContent;
      searchData.unshift({ values })
      console.log(searchData);
      wx.setStorageSync('searchData', searchData);
    }
  },
  //历史搜索 再次请求
  historySearchFun:function(e){
    var values = e.currentTarget.dataset.name;
    console.log(e)
    this.searchDataFun(values);
    if (values !== '') {
      var searchData = this.data.historyContent;
      searchData.unshift({ values })
      console.log(searchData);
      wx.setStorageSync('searchData', searchData);
    }
  },
  //取消返回上一页
  cancelFun:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  onReady: function () {

  },

  // onShow: function () {

  // },
  onHide: function () {

  },
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})